package com.returnsoft.security.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.ejb.Stateless;

import com.returnsoft.security.dto.DepartmentDto;
import com.returnsoft.security.dto.PermissionDto;
import com.returnsoft.security.dto.PersonDto;
import com.returnsoft.security.dto.UserDto;
import com.returnsoft.security.entity.Department;
import com.returnsoft.security.entity.Permission;
import com.returnsoft.security.entity.Person;
import com.returnsoft.security.entity.User;
import com.returnsoft.security.exception.ConversionExcepcion;

@Stateless
public class Conversion implements Serializable {

	private static final long serialVersionUID = 1L;

	public DepartmentDto fromDepartment(Department d)
			throws ConversionExcepcion {
		try {
			DepartmentDto dd = new DepartmentDto();
			dd.setId(d.getId());
			dd.setName(d.getName());
			if (d.getDepartment() != null) {
				DepartmentDto departmentDto = fromDepartment(d.getDepartment());
				dd.setDepartment(departmentDto);
			}
			return dd;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	public PersonDto fromPerson(Person p) throws ConversionExcepcion {
		try {
			PersonDto pd = new PersonDto();
			pd.setId(p.getId());
			pd.setFirstname(p.getFirstname());
			pd.setLastname(p.getLastname());
			pd.setDocumentNumber(p.getDocumentNumber());
			if (p.getDepartment() != null) {
				DepartmentDto department = fromDepartment(p.getDepartment());
				pd.setDepartment(department);
			}

			return pd;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	public PermissionDto fromPermission(Permission p)
			throws ConversionExcepcion {
		try {
			PermissionDto pd = new PermissionDto();
			pd.setId(p.getId());
			pd.setCode(p.getCode());
			pd.setName(p.getName());
			return pd;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}
	
		

	public UserDto fromUser(User user) throws ConversionExcepcion {
		try {
			// SE PARCEA USER
			UserDto userDto = new UserDto();
			userDto.setUsername(user.getUsername());
			userDto.setPassword(user.getPassword());
			userDto.setId(user.getId());
			userDto.setIsActive(user.getIsActive());
			if (userDto.getPerson() != null && userDto.getPerson().getId()>0) {
				System.out.println("la persona es"+userDto.getPerson().getId());
				PersonDto personDto = new PersonDto();
				personDto.setId(userDto.getPerson().getId());
				userDto.setPerson(personDto);
			}else{
				System.out.println("la persona es null");
			}
			if (user.getPermissions() != null && user.getPermissions().size() > 0) {
				List<PermissionDto> permissionsDto = new ArrayList<PermissionDto>();
				for (Permission permission : user.getPermissions()) {
					PermissionDto permissionDto = new PermissionDto();
					permissionDto.setId(permission.getId());
					permissionsDto.add(permissionDto);
				}
				userDto.setPermissions(permissionsDto);
			}

			if (user.getDepartments() != null && user.getDepartments().size() > 0) {
				List<DepartmentDto> departmentsDto = new ArrayList<DepartmentDto>();
				for (Department department : user.getDepartments()) {
					DepartmentDto departmentDto = new DepartmentDto();
					departmentDto.setId(department.getId());
					departmentsDto.add(departmentDto);
				}
				userDto.setDepartments(departmentsDto);
			}

			return userDto;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	// ///
	public Person toPerson(PersonDto pd) throws ConversionExcepcion {
		try {
			Person p = new Person();
			p.setId(pd.getId());
			p.setFirstname(pd.getFirstname());
			p.setLastname(pd.getLastname());
			p.setDocumentNumber(pd.getDocumentNumber());

			if (pd.getDepartment() != null) {
				Department department = new Department();
				department.setId(pd.getDepartment().getId());
				p.setDepartment(department);
			}

			return p;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	public User toUser(UserDto u) throws ConversionExcepcion {
		try {
			// SE PARCEA USER
			User ud = new User();
			ud.setId(u.getId());
			ud.setUsername(u.getUsername());
			ud.setPassword(u.getPassword());
			ud.setIsActive(u.getIsActive());
			// SE PARCEA PERMISSIONS
			if (u.getPermissions() != null && u.getPermissions().size() > 0) {
				Set<Permission> permissions = new HashSet<Permission>();
				for (PermissionDto permissionDto : u.getPermissions()) {
					Permission permission = new Permission();
					permission.setId(permissionDto.getId());
					permissions.add(permission);
				}
				ud.setPermissions(permissions);
			}

			if (u.getDepartments() != null && u.getDepartments().size() > 0) {
				Set<Department> departments = new HashSet<Department>();
				for (DepartmentDto departmentDto : u.getDepartments()) {
					Department department = new Department();
					department.setId(departmentDto.getId());
					departments.add(department);
				}
				ud.setDepartments(departments);
			}

			if (u.getPerson() != null) {
				Person person = new Person();
				person.setId(u.getPerson().getId());
				person.setFirstname(u.getPerson().getFirstname());
				person.setLastname(u.getPerson().getLastname());
				person.setDocumentNumber(u.getPerson().getDocumentNumber());

				if (u.getPerson().getDepartment() != null) {
					Department department = new Department();
					department.setId(u.getPerson().getDepartment().getId());
					person.setDepartment(department);
				}
				ud.setPerson(person);
			}

			return ud;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

}
